const {join}=require('path');
const fs=require('fs');
const http=require('http');
const url=require('url');

const webRoot = join(__dirname,'www');
// console.log(webRoot);
//当前脚本运行目录 D:\phpstudy_pro\WWW\0518\www
http.createServer((req,res)=>{
  if(req.url !='/favicon.ico'){
    //得到请求的对象服务器中真实的文件路径
    let pathname =req.url ==='/'?'/index.html':req.url;
  let filepath =join(webRoot,pathname);
  //pathname  D:\phpstudy_pro\WWW\0518\www\index.html
  if(fs.existsSync(filepath)){
    res.end(fs.readFileSync(filepath))
  }else{
    let error=join(webRoot,'/error/404.html')
    res.end(fs.readFileSync(error))
    
  } 
}  
}).listen(6060,'0.0.0.0',()=>{
  console.log("服务器已启动 http://127.0.0.1:6060")
})